<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/28
 * Time: 16:15
 */

namespace Services;

use Helpers\Tool;
use Helpers\Main;
use Helpers\HttpClient;
use Helpers\HttpResponse;
use Helpers\XhException;

class ResPrivilege
{
    /**
     * @var \Helpers\HttpClient
     */
    public static $httpClient;
    public static $url;

    /**
     * 初始化配置
     *
     * @throws \Exception
     *
     * create by qgz 20180628
     */
    private static function init()
    {
        if (!self::$httpClient) {
            $config    = Main::getConfigByName('resServerInterface');//接口资源中心
            self::$url = Tool::getValue($config, 'questionPrivilegeUrl'); //资源权限接口
            if (!self::$url) {
                throw new \Exception('资源权限接口url获取失败');
            }
            self::$httpClient = new HttpClient(self::$url);
        }
    }

    /**
     * 关联教书和书本
     *
     * @param array $params
     *
     * @return array|bool
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180628
     */
    public static function connectTeaBook($params = [])
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/userBookPowers/list', $params);
        return self::getResult($response);
    }

    /**
     * 撤销关联教师和书本
     *
     * @param array $params
     *
     * @return array|bool
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180703
     */
    public static function repealConnect($params = [])
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/userBookPowers/updateStatus', $params);//status -1删除 1启用 2禁用
        return self::getResult($response);
    }

    /**
     * 关联学校和书本
     *
     * @param array $params
     *
     * @return array|bool
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180706
     */
    public static function connectTeaSchool($params = [])
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/bookSchoolScopes', $params);
        return self::getResult($response);
    }

    /**
     * 撤销关联学校和书本
     *
     * @param array $params
     *
     * @return array|bool
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180928
     * modify by qgz 20181011 使用新接口
     */
    public static function repealConnectSchool($params = [])
    {
        self::init();
//        $response = self::$httpClient->delete("/api/v1/bookSchoolScopes/{$schoolId}/books/{$bookId}", $params);
        $response = self::$httpClient->postJson('/api/v1/bookSchoolBlacks', $params);
        return self::getResult($response);
    }

    /**
     * 检查错误并获取结果
     *
     * @param HttpResponse $response
     *
     * @throws XhException
     * @return array|bool
     *
     * create by ck 20180604
     */
    public static function getResult(HttpResponse $response)
    {
        if (!$response->isSuccess()) {
            throw new XhException('操作失败', $response);
        }
        $body = $response->toArray();
        if (isset($body['code']) && $body['code'] != 200 && $body['code'] != 201) {
            throw new XhException(Tool::getValue($body, 'msg', '请求失败'), $response);
        }
        return Tool::getValue($body, 'results', true);
    }
}
